//
//  KKCircleProgressView.m
//  WritingBoard
//
//  Created by Mac mini on 16/1/23.
//  Copyright © 2016年 JuniorCodingPerson. All rights reserved.
//

#import "KKCircleProgressView.h"

static inline CGFloat KKCircleProgressRadians(double degreds){
    return degreds * M_PI * 2;
}

@implementation KKCircleProgressView

- (instancetype)initWithFrame:(CGRect)frame circleWidth:(CGFloat)circleWidth currentValue:(double)currentValue totalValue:(double)totalValue {
    self = [super initWithFrame: frame];
    if (self) {

        self.backgroundColor = [UIColor whiteColor];
        
        _kk_currentValue = currentValue;
        _kk_totalValue = totalValue;
        _kk_progressTintColor = [UIColor darkGrayColor];
        _kk_trackTintColor = [UIColor lightGrayColor];
        _kk_centerColor = self.backgroundColor;
        _kk_circleWidth = circleWidth;
        
        _kk_progressLabel = [[UILabel alloc] initWithFrame: [self bounds]];
        _kk_progressLabel.textAlignment = NSTextAlignmentCenter;
        _kk_progressLabel.font = [UIFont systemFontOfSize: 14];
        _kk_progressLabel.textColor = [UIColor blackColor];
        [self addSubview: _kk_progressLabel];
        
    }
    return self;
}

#pragma mark - Setter methods

- (void)setKk_circleWidth:(CGFloat)kk_circleWidth {
    _kk_circleWidth = kk_circleWidth;
    [self setNeedsDisplay];
}

- (void)setKk_progressTintColor:(UIColor *)kk_progressTintColor {
    _kk_progressTintColor = kk_progressTintColor;
    [self setNeedsDisplay];
}

- (void)setKk_trackTintColor:(UIColor *)kk_trackTintColor {
    _kk_trackTintColor = kk_trackTintColor;
    [self setNeedsDisplay];
}

- (void)setKk_centerColor:(UIColor *)kk_centerColor {
    _kk_centerColor = kk_centerColor;
    [self setNeedsDisplay];
}

- (void)setKk_currentValue:(double)kk_currentValue {
    [self setProgress: kk_currentValue animated: false];
}

- (void)setKk_totalValue:(double)kk_totalValue {
    _kk_totalValue = kk_totalValue;
    [self setNeedsDisplay];
}

#pragma mark - public methods

- (void)setProgress:(double)progress animated:(BOOL)animated {
    _kk_currentValue = progress;
    
    NSInteger scale = _kk_currentValue / _kk_totalValue * 100;
    
    if (scale > 100) {
        scale = 100;
    }
    
    [_kk_progressLabel setText: [NSString stringWithFormat: @"%@",@(scale)]];
    
    [self setNeedsDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    CGPoint center = CGPointMake(CGRectGetWidth(self.bounds)/2, CGRectGetHeight(self.bounds)/2);
    CGFloat circleRadius = MIN(CGRectGetWidth(self.bounds)/2, CGRectGetHeight(self.bounds)/2) - 2;
    
    CGContextRef context = UIGraphicsGetCurrentContext();//获得当前view的图形上下文(context)
    
    //  Track
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, center.x, center.y);
    CGContextAddArc(context, center.x, center.y, circleRadius, -M_PI_2, -M_PI_2 + M_PI * 2, 0);
    CGColorRef color = self.kk_trackTintColor.CGColor;
    CGContextSetFillColorWithColor(context, color);
    CGContextFillPath(context);
    
    
    //  progress
    CGFloat progressAngle = KKCircleProgressRadians(_kk_currentValue / _kk_totalValue);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, center.x, center.y);
    CGContextAddArc(context, center.x, center.y, circleRadius, -M_PI_2, -M_PI_2 + progressAngle, 0);
    color = self.kk_progressTintColor.CGColor;
    CGContextSetFillColorWithColor(context, color);
    CGContextFillPath(context);
    
    
    //  区域内
    int innerDiameter = circleRadius * 2 - _kk_circleWidth;
    float innerRadius = innerDiameter / 2;
    
    CGContextSetLineWidth(context, _kk_circleWidth);
    CGRect innerCircle = CGRectMake(center.x - innerRadius, center.y - innerRadius,
                                    innerDiameter, innerDiameter);
    CGContextAddEllipseInRect(context, innerCircle);
    CGContextClip(context);
    CGContextClearRect(context, innerCircle);
    CGContextSetFillColorWithColor(context, self.kk_centerColor.CGColor);
    CGContextFillRect(context, innerCircle);
}

@end
